<?php
function getAverage($value, $played) {
    return $played > 0 ? round($value / $played, 2) : 0;
}

function echoHighLowGame($titleHigh, $highGame, $titleLow, $lowGame) {
    echo<<< EOQ
              <th width="30%"><a href="match_view.php?matchId=$highGame->matchId">$titleHigh</a></th>
              <th>$highGame->myValue&nbsp;-&nbsp;$highGame->opponentValue</th>
              <th width="30%"><a href="match_view.php?matchId=$lowGame->matchId">$titleLow</a></th>
              <th>$lowGame->myValue&nbsp;-&nbsp;$lowGame->opponentValue</th>
EOQ;
}

$showStats = true;
include_once ("includes/header.php");

$id = getParam("id");
$team = $teams->getTeam($id);
$teamStats = new TeamStats($team);

$netTds = $teamStats->getNetTDs();
$netCas = $teamStats->getNetCas();

$aveTdFor = $teamStats->getAverageScore();
$aveTdAg = $teamStats->getAverageConceded();
$aveTdNet = $teamStats->getAverageNetTD();

$aveCasFor = $teamStats->getAverageInflicted();
$aveCasAg = $teamStats->getAverageSuffered();
$aveCasNet = $teamStats->getAverageNetCas();

echo<<< EOQ
    <caption><a href="team_viewer.php?id=$team->id"><h1>$team->name Team Stats</h1></a></caption>
    <tr><td><table border="0" cellPadding="5" cellSpacing="0" width="100%">
            <tr align="center">
              <th colspan="6">Matches</th>
            </tr>\n
            <tr class="row2" align="center">
              <th>Played</th>
              <th>Won</th>
              <th>Drawn</th>
              <th>Lost</th>
              <th>Net Tds</th>
              <th>Net Cas</th>
            </tr>\n
            <tr align="center">
              <td>$teamStats->played</td>
              <td>$teamStats->won</td>
              <td>$teamStats->drawn</td>
              <td>$teamStats->lost</td>
              <td>$netTds</td>
              <td>$netCas</td>
            </tr>\n
    </table></td></tr>

    <tr><td><table border="0" cellPadding="5" cellSpacing="0" width="100%">
            <tr align="center">
              <th colspan="6"><br /><br />Form</th>
            </tr>\n
            <tr class="row2" align="center">
              <th>&nbsp</th>
              <th>Won</th>
              <th>No Loses</th>
              <th>No Wins</th>
              <th>Lost</th>
            </tr>\n
            <tr align="center">
              <th>Current</th>
              <td>$teamStats->currentWins</td>
              <td>$teamStats->currentNoLoss</td>
              <td>$teamStats->currentNoWin</td>
              <td>$teamStats->currentLoss</td>
            </tr>\n
            <tr class="row2" align="center">
              <th>All Time</th>
              <td>$teamStats->wins</td>
              <td>$teamStats->noLoss</td>
              <td>$teamStats->noWin</td>
              <td>$teamStats->loss</td>
            </tr>\n
    </table></td></tr>

    <tr><td><table border="0" cellPadding="5" cellSpacing="0" width="100%">
            <tr align="center">
              <th colspan="7"><br /><br />Tds and Casualties</th>
            </tr>\n
            <tr class="row2" align="center">
              <th>&nbsp;</th>
              <th colspan="3">Total</th>
              <th colspan="3">Average</th>
            </tr>\n
            <tr class="row2" align="center">
              <th>&nbsp;</th>
              <th>For</th>
              <th>Against</th>
              <th>Net</th>
              <th>For</th>
              <th>Against</th>
              <th>Net</th>
            </tr>\n
            <tr align="center">
              <th>TDs</th>
              <td>$teamStats->tdFor</td>
              <td>$teamStats->tdAg</td>
              <td>$netTds</td>
              <td>$aveTdFor</td>
              <td>$aveTdAg</td>
              <td>$aveTdNet</td>
            </tr>\n
            <tr class="row2" align="center">
              <th>Casualties</th>
              <td>$teamStats->casFor</td>
              <td>$teamStats->casAg</td>
              <td>$netCas</td>
              <td>$aveCasFor</td>
              <td>$aveCasAg</td>
              <td>$aveCasNet</td>
            </tr>\n
    </table></td></tr>

    <tr><td><table border="0" cellPadding="10" cellSpacing="0" width="100%">
            <tr align="center">
              <th colspan="4"><br /><br />Record Games</th>
            </tr>\n
            <tr class="row2" align="left">
EOQ;
echoHighLowGame("Best Score", $teamStats->highTdDiff, "Worst Score", $teamStats->lowTdDiff);
echo<<< EOQ
            </tr>\n
            <tr align="left">
EOQ;
echoHighLowGame("High Score", $teamStats->highTdFor, "Low Score", $teamStats->lowTdFor);
echo<<< EOQ
            </tr>\n
            <tr class="row2" align="left">
EOQ;
echoHighLowGame("Highest Total Score", $teamStats->highTdScore, "Lowest Total Score", $teamStats->lowTdScore);
echo<<< EOQ
            </tr>\n
            <tr align="left">
EOQ;
echoHighLowGame("Best Casualties", $teamStats->highCasDiff, "Worst Casualties", $teamStats->lowCasDiff);
echo<<< EOQ
            </tr>\n
            <tr class="row2" align="left">
EOQ;
echoHighLowGame("High Casualties", $teamStats->highCasFor, "Low Casualties", $teamStats->lowCasFor);
echo<<< EOQ
            </tr>\n
            <tr align="left">
EOQ;
echoHighLowGame("Highest Total Casualties", $teamStats->highCasScore, "Lowest Total Casualties", $teamStats->lowCasScore);
echo<<< EOQ
            </tr>\n
    </table></td></tr>
EOQ;
include ("includes/footer.php");
?>
